package com.nd.mobile.study.view.helper;

import android.content.Context;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.CharacterStyle;
import android.util.Pair;

public class TextHelper {
	
	private final static String PATTERN_COLOR = "<font color=\"#{color}\">$0</font>";
	
	/*
	 * Pair<String, CharacterStyle> CharacterStyle代表相应String的Style
	 * 如果只是颜色可以传入ForegroundColorSpan,如果有颜色有大小可以传入TextAppearanceSpan
	 * 注意: 同一个CharacterStyle对象只能被使用一次
	 */
	public static Spannable buildRichText (Pair<Object, CharacterStyle>... textAndStyle) {
		
		SpannableStringBuilder result = new SpannableStringBuilder();
		
		int preResultLen = 0;
		for (Pair<Object, CharacterStyle> currPair : textAndStyle) {
			preResultLen = result.length();
			
			if (null != currPair.first) {
				result.append(currPair.first.toString()); // currPair.first is text
				if (null != currPair.second) // currPair.second is style
					result.setSpan(currPair.second, preResultLen, result.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
			}
		}
		return result;
	}
	
	private static String buildColoPattern (Context context, int resColor){
		return PATTERN_COLOR.replace("{color}", context.getString(resColor).substring(3));
	}
	
	private static String buildColoPattern (String color){
		return PATTERN_COLOR.replace("{color}", color.substring(3));
	}
	
	/**
	 * 给所有数字上色
	 * @param source 原始字符串
	 * @param color 颜色为Html格式, 形如"123456"
	 * @return
	 */
    public static String colorNum(String source, String color) {
        String result = source.replaceAll("\\d+", buildColoPattern(color));
        return result;
    }
    
    /**
     * 给所有数字上色
     * @param source 原始字符串
     * @param resColor e.g: R.color.black
     * @param context which is used to call getString(R.color.xxx)
     * @return
     */
    public static String colorNum(String source, int resColor, Context context) {
    	String result = source.replaceAll("\\d+", buildColoPattern(context, resColor));
    	return result;
    }
    
    /**
     * 给所有数字占位符(即"%d")上色 
     * @param source 原始字符串
     * @param color 颜色为Html格式, 形如"123456"
     * @return
     */
    public static String colorNumPlaceHolder(String source, String color) {
    	String result = source.replaceAll("\\%d+", buildColoPattern(color));
    	return result;
    }
    
    /**
     * 给所有数字占位符(即"%d")上色 
     * @param source 原始字符串
     * @param resColor e.g: R.color.black
     * @param context which is used to call getString(R.color.xxx)
     * @return
     */
    public static String colorNumPlaceHolder(String source, int resColor, Context context) {
    	String result = source.replaceAll("\\%d+", buildColoPattern(context, resColor));
    	return result;
    }
    
    /**
     * 给所有数字占位符(即"%d")上色 
     * @param resString e.g: R.string.str
     * @param color 颜色为Html格式, 形如"123456"
     * @param context which is used to call getString(R.color.xxx)
     * @return
     */
    public static String colorNumPlaceHolder(int resString, String color, Context context) {
    	String result = context.getString(resString).replaceAll("\\%d+", buildColoPattern(color));
    	return result;
    }
    
    /**
     * 给所有数字占位符(即"%d")上色 
     * @param resString e.g: R.string.str
     * @param resColor e.g: R.color.black
     * @param context which is used to call getString(R.color.xxx)
     * @return
     */
    public static String colorNumPlaceHolder(int resString, int resColor, Context context) {
    	String result = context.getString(resString).replaceAll("\\%d+", buildColoPattern(context, resColor));
    	return result;
    }
}
